package com.jg.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author 86130
 * @version 1.0
 * @description: TODO
 * @date 2025/6/6 上午11:12
 */
@Component
@Aspect
public class MyAdvice {
    @Pointcut("execution(public * com.jg.service.*.*(..))")
    public void pt(){}

    @Before("pt()")
    public void before(JoinPoint point){
        System.out.println("前置通知");
        System.out.println("调用时间:"+System.currentTimeMillis());
        System.out.println("类名:"+point.getSignature().getDeclaringType().getName());
        Object[] args = point.getArgs();
        for (Object arg : args) {
            System.out.println("参数:"+arg);
        }
        System.out.println("方法名:"+point.getSignature().getName());
    }

    @After("pt()")
    public void after(JoinPoint point){
        System.out.println("后置通知");
        System.out.println("调用时间:"+System.currentTimeMillis());
    }

    //@Around("pt()")
    public void around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("环绕通知");
        System.out.println("类名:"+point.getSignature().getDeclaringType().getName());
        System.out.println("方法名:"+point.getSignature().getName());
        Object[] args = point.getArgs();
        for (Object arg : args) {
            System.out.println("参数:"+arg);
        }
        System.out.println("调用时间:"+System.currentTimeMillis());
        point.proceed();
        System.out.println("结束时间:"+System.currentTimeMillis());
    }

    //@AfterThrowing("pt()", throwing = "e")
    public void afterThrowing(JoinPoint point,Exception e){
        System.out.println("异常通知");
        System.out.println("类名:"+point.getSignature().getDeclaringType().getName());
        System.out.println("方法名:"+point.getSignature().getName());
        System.out.println("异常信息:"+e.getMessage());
    }
}
